const enemyConfig = require('../../scripts/configs/enemyConfig');
var Game = require('Game');

cc.Class({
    extends: cc.Component,

    properties: {
        

        enemyPrefab: {
            default: null,
            type: cc.Prefab
        },

        enemyInfoLb: {
            default: null,
            type: cc.Label
        },

        loadEnemyCount: 0,
        onLineEnemyCount: 0,
        allEnemyCount: 0,
    },

    getEnemyCurConfig() {
        if (enemyConfig.enemyLevels[this.enemyLevel]) {
            return JSON.parse(JSON.stringify(enemyConfig.enemyLevels[this.enemyLevel]));
        }
        throw '敌人等级错误' + this.enemyLevel;
    },

    setEnemyShowInfoLb(){
        if (this.enemyInfoLb != null) {
            this.enemyInfoLb.string = "怪物等级：" + (this.enemyLevel + 1) + "\n击杀怪物：" + (this.loadEnemyCount - this.onLineEnemyCount) + "/" + this.allEnemyCount;
        }
    },

    newEnemy(){
        if (this.enemyPool.size() > 0) {
            return this.enemyPool.get();
        }else{
            return cc.instantiate(this.enemyPrefab);
        }
    },

    drawEnemy(){

        if(this.game.gameOver)return;

        let enemyInfo = this.getEnemyCurConfig();

        this.allEnemyCount = enemyInfo.num;

        if(this.loadEnemyCount >= enemyInfo.num)
        {
            if (this.onLineEnemyCount <= 0) {
                this.enemyLevel++;
                if (this.enemyLevel > enemyConfig.enemyLevels.length - 1) {
                    this.enemyLevel = enemyConfig.enemyLevels.length - 1;
                }
                this.game.drawEffect(this.node, 1, 'message1', '怪物升级!!!');

                this.loadEnemyCount = 0;

                this.setEnemyShowInfoLb();

                this.drawEnemy();
            }

            return;
        }

        let enemyCount  = enemyInfo.oneNumMin + Math.floor(Math.random() * (enemyInfo.oneNumMax - enemyInfo.oneNumMin));

        let enemyCountEnd = Math.min(enemyCount, enemyInfo.FullNumMax - this.onLineEnemyCount);

        for (var i = 0; i < enemyCountEnd; i++) {

            let enemy = this.newEnemy();

            if(enemy == null) continue;
            // 将新增的节点添加到 Canvas 节点下面
            this.node.addChild(enemy);

            var maxX = this.node.width / 2;

            let randX = (Math.random() - 0.5) * 2 * maxX;

            enemy.setPosition(randX, this.node.height / 2 - 20);

            enemy.getComponent('Enemy').initInfo(enemyInfo, this);

            this.onLineEnemyList.push(enemy);

            this.loadEnemyCount++;

            this.onLineEnemyCount++;
        }

        this.setEnemyShowInfoLb();
        
    },

    delEnemy(enemy){
        this.enemyPool.put(enemy);

        this.onLineEnemyCount--;

        this.setEnemyShowInfoLb();

        this.drawEnemy();
    },

    setGameOver(){
        for (var i = this.onLineEnemyList.length - 1; i >= 0; i--) {
            this.delEnemy(this.onLineEnemyList[i]);
        }
        this.onLineEnemyList.length = 0;

        this.enemyLevel = 0;

        this.loadEnemyCount= 0;
        this.onLineEnemyCount= 0;
        this.allEnemyCount= 0;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.game = Game.getInstance();
        this.enemyPool = new cc.NodePool();
        let enemy = cc.instantiate(this.enemyPrefab);
        this.enemyPool.put(enemy);

        this.onLineEnemyList = new Array();

        this.enemyLevel = 0;
    },

    start () {

    },

    // update (dt) {},
});
